﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BBPA.BLL
{
   public class GroupBuy
    {
        private readonly BBPA.DAL.GroupBuy dal = new BBPA.DAL.GroupBuy();
        public GroupBuy()
        { }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int gbid)
        {
            return dal.Exists(gbid);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(BBPA.Model.GroupBuy model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(BBPA.Model.GroupBuy model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int gbid)
        {

            return dal.Delete(gbid);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string gbidlist)
        {
            return dal.DeleteList(gbidlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BBPA.Model.GroupBuy GetModel(int gbid)
        {

            return dal.GetModel(gbid);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public BBPA.Model.GroupBuy GetModelByCache(int gbid)
        {

            string CacheKey = "GroupBuyModel-" + gbid;
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(gbid);
                    if (objModel != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (BBPA.Model.GroupBuy)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<BBPA.Model.GroupBuy> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<BBPA.Model.GroupBuy> DataTableToList(DataTable dt)
        {
            List<BBPA.Model.GroupBuy> modelList = new List<BBPA.Model.GroupBuy>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                BBPA.Model.GroupBuy model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage_order(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage_order(strWhere, orderby, startIndex, endIndex);
        }

        public void CheckGroupBuy()
        { 
            dal.Plan_CheckGroupBuy();
        }

        public DataTable GetOutGroupBuy()
        {
           return dal.GetOutGroupBuy();
        }

        //public void Plan_CheckGroupBuy()
        //{

        //    BBPA.BLL.GroupBuy bll = new BBPA.BLL.GroupBuy();
        //    DataTable dt = bll.GetOutGroupBuy();

        //    foreach (DataRow dr in dt.Rows)
        //    {
        //        BBPA.Model.Orders model = new BBPA.Model.Orders();
        //        BBPA.BLL.Orders bll_order = new BBPA.BLL.Orders();
        //        BBPA.BLL.Store bllStore = new BBPA.BLL.Store();
        //        BBPA.BLL.V_Orders bllList = new BBPA.BLL.V_Orders();
        //        BBPA.BLL.Orders_State_Log bllOrders_State_Log = new BBPA.BLL.Orders_State_Log();
        //        //List<BBPA.Model.V_Orders> modellist = bllList.GetModelList(" gbid=" + Master.id);

        //        if (Convert.ToInt32(dr["ExpectPay"]) > 0)
        //        {
        //            model = bll_order.GetModel(Convert.ToInt32(dr["id"]));
        //            BBPA.Model.Store modelStore = bllStore.GetModel(model.StoreID);
        //            //1.支付货款
        //            CallBak<int> callBak_BuySuccess = BBPA.BLL.Account_Change_Log.Add(EnumHelper.UserType.Store, Convert.ToInt32(dr["StoreID"]), EnumHelper.AccountChangeReason.Cancel_Orders, Convert.ToInt32(dr["id"]), 0);//扣孕婴店


        //            //2.扣运费
        //            CallBak<int> callBak_PayExpressMoney = BBPA.BLL.Account_Change_Log.Add(EnumHelper.UserType.Store, modelStore.id, EnumHelper.AccountChangeReason.Pay_ExpressMoney, Convert.ToInt32(dr["id"]), 0);


        //            model.ExpressID = 0;
        //            model.ExpressPrice = 0;
        //            model.ExpressNumber = "";
        //            model.State = (int)EnumHelper.OrdersState.NoGoods;
        //            model.DateandtimeSend = DateTime.Now;
        //            model.IsBuyMoneyOK = true;
        //            bll_order.Update(model);

        //            BBPA.BLL.User_Operate_Log.Add(Convert.ToInt32(dr["UserID"]), EnumHelper.TableName.Orders, Convert.ToInt32(dr["gbid"]), EnumHelper.OperateType.Update, "");
        //            //订单状态日志
        //            BBPA.Model.Orders_State_Log modelOrders_State_Log = new BBPA.Model.Orders_State_Log();
        //            modelOrders_State_Log.OrdersID = Convert.ToInt32(dr["id"]);
        //            modelOrders_State_Log.State = model.State;
        //            int Orders_State_LogID_new = bllOrders_State_Log.Add(modelOrders_State_Log);
        //            BBPA.BLL.User_Operate_Log.Add(Convert.ToInt32(dr["UserID"]), EnumHelper.TableName.Orders_State_Log, Orders_State_LogID_new, EnumHelper.OperateType.Add, "");
        //            //扣除运费后，孕婴店金额发生变化，所以要重新获取modelStore
        //            modelStore = bllStore.GetModel(model.StoreID);
        //            //通知孕婴店
        //            BBPA.BLL.Message bllMessage = new BBPA.BLL.Message();
        //            bllMessage.NewMessage(0, Convert.ToInt32(dr["UserID"]), modelStore.UserID,
        //                "您参与的团购已失败，共计金额" + StringPlus.showMoney(model.ExpectPay) + "元，运费100元，已返回您的账户，您帐户剩余金额为" + modelStore.Account + "元", true, true);

        //        }

        //        bll_order.Delete(Convert.ToInt32(dr["id"]));

        //        dal.DelOrders_Goods(Convert.ToInt32(dr["id"]));

        //    }
        //}
    }
}
